package com.yy.cloud.controller;

import com.yy.cloud.entity.GoodsInfo;
import com.yy.cloud.param.ComplexModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品服务controller
 *
 * @date 2024/5/6
 */
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsServiceController {

    @Value("${server.port}")
    private String serverPort;

    /**
     * 模拟根据商品id查询商品详情
     *
     * @param goodsId 商品id
     * @return 商品详情
     */
    @GetMapping("/{goodsId}")
    public String goodsDetail(@PathVariable("goodsId") int goodsId) {
        if (goodsId < 1 || goodsId > 10000) {
            return "查询商品为空，当前服务的端口号为：" + serverPort;
        }
        String goodsName = "商品" + goodsId;
        return goodsName + ",当前服务的端口号为：" + serverPort;
    }

    /**
     * 查询商品详情
     *
     * @param sellStatus 商品上架状态
     * @param goodsId    商品id
     * @return 商品详情信息
     */
    @GetMapping("/detail")
    public String goodsDetailByParams(@RequestParam("sellStatus") int sellStatus, @RequestParam("goodsId") int goodsId) {
        log.info("入参：sellStatus={},goodsId={}", sellStatus, goodsId);
        if (goodsId < 1 || goodsId > 100000) {
            return "goodsDetailByParams 查询商品为空，当前服务的端口号为：" + serverPort;
        }
        String goodsName = "商品：" + goodsId + ",上架状态：" + sellStatus;
        return "goodsDetailByParams " + goodsName + ",当前服务的端口号为：" + serverPort;
    }

    /**
     * 查询多个商品详情
     *
     * @param goodsIds 商品id数组
     * @return 商品详情数组
     */
    @GetMapping("/listByIdArray")
    public String[] listByIdArray(@RequestParam("goodsIds") Integer[] goodsIds) {
        if (goodsIds.length < 1) {
            return null;
        }
        String[] goodsInfos = new String[goodsIds.length];
        for (int i = 0; i < goodsInfos.length; i++) {
            goodsInfos[i] = "商品：" + goodsIds[i];
        }
        return goodsInfos;
    }

    /**
     * 查询多个商品详情
     *
     * @param goodsIds 商品id集合
     * @return 商品详情List
     */
    @GetMapping("/listByIdList")
    public List<String> listByIdList(@RequestParam("goodsIds") List<Integer> goodsIds) {
        if (CollectionUtils.isEmpty(goodsIds)) {
            return null;
        }
        List<String> goodsInfos = new ArrayList<String>();
        for (Integer goodsId : goodsIds) {
            goodsInfos.add("商品：" + goodsId);
        }
        return goodsInfos;
    }

    /**
     * 模拟修改商品的库存信息
     *
     * @param goodsInfo 商品详情
     * @return 新的商品信息
     */
    @PostMapping("/updateGoodsInfo")
    public GoodsInfo updateGoodsInfo(@RequestBody GoodsInfo goodsInfo) {
        if (goodsInfo.getGoodsId() > 0) {
            goodsInfo.setStock(goodsInfo.getStock() - 1); // 库存-1
        }
        return goodsInfo;
    }


    @PostMapping("/testComplexModel")
    public ComplexModel testComplexModel(@RequestBody ComplexModel complexModel) {
        complexModel.setRequestNum(complexModel.getRequestNum() - 1);
        return complexModel;
    }
}
